<template>
	<color-header height="900rpx" page-color="#fff">
		<image class="mine-bg" src="/static/images/mine-bg.png" mode="aspectFill"></image>
	</color-header>
	<uni-nav-bar title="账户登录" left-icon="left" clear-float status-bar background-color="transparent" :border="false"
	 @clickLeft="$navTo('',{}, 'navigateBack')"></uni-nav-bar>	
	<view class="login-wrapper">
		<image class="logo" src="/static/images/mine-empty.png" mode="widthFix"></image>
		<primary-btn class="mobile-btn" title="手机号快捷登录" :showoutborder="false" bg-color="#00CAC0"  @btnClick="mobileLogin"></primary-btn>
		<view class="login-btn">
			<button class="btn login" hover-class="tap-hover" open-type="getPhoneNumber" @getphonenumber="login">一键登录</button>
		</view>
	</view>
	
	<uni-popup ref="mobilePopup" type="bottom" :safe-area="false" :is-mask-click="false">
		<view class="mobile-login-wrapper">
			<view class="close-icon" @click="$popupChange(this, 'mobilePopup')"><uni-icons type="closeempty" :size="24" color="#999"></uni-icons></view>
			<view class="mobile-title">手机号快捷登录</view>
			<image class="logo" src="" mode="aspectFill"></image>
			<view class="form-item">
				<input class="form-ipt" type="number" v-model="phone" placeholder="请输入手机号" maxlength="11" />
			</view>
			<view class="form-item code-item">
				<input class="code-ipt" type="number" placeholder="请输入手机验证码" v-model="code" />
				<view class="code-btn" hover-class="tap-hover" @click="getCode">{{ codeText }}</view>
			</view>
			<checkbox-group @change="checkChange">
				<view class="protocol-wrapper">
					<label class="label-check">
						<checkbox class="circle" style="transform: scale(0.8);" :value="1"
						activeBackgroundColor="#00CAC0" activeBorderColor="#00CAC0" iconColor="#fff"  />
						<text>已阅读并同意</text>
					</label>
					<text class="theme-color">《See Wow用户协议》</text>、
					<text class="theme-color">《See Wow隐私政策》</text>
				</view>
			</checkbox-group>
			
			<primary-btn class="mobile-confirm-btn" title="确定"></primary-btn>
		</view>
	</uni-popup>
</template>

<script>
	import { useUserStore } from '../../store/userStore';
	export default {
		data() {
			return {
				codeText: '',
				code: '',
				phone: '',
				userStore: useUserStore(),
				wx_code: '', // 登录所需的code
			}
		},
		computed: {
			mobilePopupHeight() {
				return uni.getWindowInfo().windowHeight - 20 + 'px'
			}
		},
		onLoad() {
			uni.login({
				provider: 'weixin',
				success: (res) => {
					// console.log(res);
					this.wx_code = res.code
				}
			})
		},
		methods: {
			async login(e) {
				if (!e.detail?.iv) {
					uni.showToast({
						title: '授权失败',
						icon: 'none'
					})
					return
				}
				
				const { encryptedData,iv } = e.detail
				const res = await this.$request.post('/api/login/login', {
					type: 3,
					wx_code: this.wx_code,
					encrypted_data: encryptedData,
					iv: iv
				})
				// console.log(res);
				if (res.code == 200) {
					uni.setStorageSync('userInfo', res.data)
					uni.setStorageSync('history-user', res.data)
					uni.setStorageSync('token', res.data.token)
					this.userStore.userInfo = res.data
					uni.$emit('loginSuccess')
					setTimeout(() => {
						uni.navigateBack()
					}, 200)
				} else {
					uni.showToast({
						title: '登录失败',
						icon: 'none'
					})
				}
			},
			mobileLogin() {
				uni.navigateTo({
					url: '/pages/login/mobileLogin',
					success: (res) => {
						res.eventChannel.emit('wxCode',{
							wx_code: this.wx_code
						})
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #F1F6F7;
	}
</style>
<style scoped lang="scss">
	.mine-bg {
		width: 100%;
		height: 900rpx;
	}
	.login-wrapper {
		width: 100%;
		padding: 0 44rpx;
		display: flex;
		position: fixed;
		z-index: 10;
		left: 0;
		right: 0;
		bottom: 300rpx;
		flex-direction: column;
		align-items: center;
		
		.login-btn {
			width: 100%;
			margin-top: 32rpx;
			border-radius: 60rpx;
			padding: 4rpx;
			// background: linear-gradient(to bottom, #E3FFF2, #9FEFE7);
			.login {
				width: 100%;
				height: 88rpx;
				border-radius: 50rpx;
				// background: linear-gradient(30deg,#08C0CC 40%, #4DE2D2, #E7E0FD);
				border: 1px solid $uni-color-primary;
				color: $uni-color-primary;
				font-size: 34rpx;
				font-weight: 500;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
		
		.mobile-btn {
			width: 100%;
		}
	}
	
	.logo {
		width: 380rpx;
		margin-top: 160rpx;
		margin-bottom: 60rpx;
	}

	.mobile-login-wrapper {
		background-color: #fff;
		border-radius: 32rpx 32rpx 0 0;
		position: relative;
		height: v-bind(mobilePopupHeight);
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 24rpx 40rpx;
		
		.mobile-confirm-btn {
			width: 100%;
			margin-top: 60rpx;
		}
		.close-icon {
			position: absolute;
			top: 20rpx;
			right: 20rpx;
		}
		.mobile-title {
			font-weight: 500;
			text-align: center;
		}
		
		.form-item {
			box-sizing: border-box;
			margin-top: 32rpx;
			width: 100%;
			height: 88rpx;
			padding: 0 24rpx;
			display: flex;
			align-items: center;
			background-color: #f6f7f8;
			border-radius: 20rpx;
			.form-ipt {
				width: 100%;
			}
		}
		.code-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			.code-btn {
				font-size: 26rpx;
				color: $uni-color-primary;
				background-color: rgba($color: $uni-color-primary, $alpha: 0.1);
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 16rpx;
				width: 160rpx;
				text-align: center;
			}
		}
		.protocol-wrapper {
			margin-top: 20rpx;
			width: 100%;
			text-align: left;
			font-size: 28rpx;
			.theme-color {
				color: $uni-color-primary;
			}
			.label-check {
				display: inline-flex;
				align-items: center;
			}
			
		}
	}

</style>